package com.woniu.order.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.order.client.OrderQueryClient;
import com.woniu.order.client.param.SearchOrderParam;
import com.woniu.order.client.result.OrderQueryResultInfo;
import com.woniu.order.infra.model.Order;
import com.woniu.order.service.OrderService;
import com.woniu.order.service.dto.OrderDto;
import com.woniu.order.service.param.SearchOrderServiceParam;
import com.woniu.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class OrderQueryApiImpl implements OrderQueryClient {

    @Resource
    private OrderService orderService;

    /**
     * 根据订单ID查询订单
     * @param orderId
     * @return
     */
    @Override
    public Result<OrderQueryResultInfo> searchOrderByOrderId(Integer orderId) {
        log.info("根据订单ID查询订单：{}", orderId);
        Order order = orderService.getById(orderId);
        OrderQueryResultInfo orderQueryResultInfo = BeanUtil.copyProperties(order, OrderQueryResultInfo.class);
        return Result.getInstance(OrderQueryResultInfo.class).setData(orderQueryResultInfo);
    }

    /**
     * 根据用户ID、订单状态查询订单
     * @param searchOrderParam
     * @return
     */
    @Override
    public Result<List<OrderQueryResultInfo>> searchOrder(SearchOrderParam searchOrderParam) {
        log.info("根据用户ID、订单状态查询订单：{}", searchOrderParam);
        SearchOrderServiceParam searchOrderServiceParam = BeanUtil.copyProperties(searchOrderParam, SearchOrderServiceParam.class);
        List<OrderDto> orderDtos = orderService.searchOrder(searchOrderServiceParam);
        List<OrderQueryResultInfo> orderQueryResultInfos = BeanUtil.copyToList(orderDtos, OrderQueryResultInfo.class);
        return Result.getInstanceList(OrderQueryResultInfo.class).setData(orderQueryResultInfos);
    }
}
